﻿using AutoMapper;
using ERP.Common.Results;
using ERP.ERPSystem.RBAC.Dto;
using ERP.Team.RBAC;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace ERP.ERPSystem.RBAC
{
    public class PermissionService : ApplicationService, IPermissionService
    {
        private readonly IRepository<PermissionModel,int> _perreposiyory;

        public PermissionService(IRepository<PermissionModel, int> perreposiyory)
        {
            _perreposiyory = perreposiyory;
        }
        /// <summary>
        /// 添加权限信息
        /// </summary>
        public async Task<int> AddPermission(PermissionDTO permissionDTO)
        {
            var  permission = ObjectMapper.Map<PermissionDTO, PermissionModel>(permissionDTO);
            var i=await _perreposiyory.InsertAsync(permission);
            return i == null ? 0 : 1;
        }
        /// <summary>
        /// 查询权限树
        /// </summary>
        /// <returns></returns>
        public async Task<List<PermissionTreeDTO>> GetTreePermission(int ParentId)
        {
            var permission =await _perreposiyory.GetListAsync(x=>x.ParentId==ParentId);
            List<PermissionTreeDTO> list = new List<PermissionTreeDTO>();
            foreach (var item in permission)
            {
              list.Add(new PermissionTreeDTO { 
              
              Id = item.Id,
              PermissionName=item.PermissionName,
              children=await GetTreePermission(item.Id)

              });
            }
            return list;
        }
        /// <summary>
        ///权限树反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public  async Task<PermissionDTO> fantianpermission(int id)
        {
            var permission=await _perreposiyory.GetAsync(x=>x.Id==id);
            var per = ObjectMapper.Map<PermissionModel,PermissionDTO>(permission);
            return per;
        }
    }
}
